<?php
class Administrator_ContactController extends Nguyen_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){
		$model = new Administrator_Model_Contact();
		//$model1 = new Administrator_Model_Product_Model();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/contact?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');	
				$diachi = $this->_request->getParam('diachi','');
				$email = $this->_request->getParam('email','');
				$website = $this->_request->getParam('website','');
				$mobile = $this->_request->getParam('mobile','');
				$phone = $this->_request->getParam('phone','');
				$fax = $this->_request->getParam('fax','');
							
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/contact/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					$model->insertContact($name, $diachi, $email, $website,$mobile, $phone, $fax, $imgname, '1');
				}
				if($task == 'edit'){
					$info =$model->GetContactInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/contact/'.$imgname)){
						$model->DeleteImageOnHost( 'contact',$info['image']);
					}
					else{
						$imgname = $info['image'];
					}
					$model->updateContact($id, $name, $diachi, $email, $website,$mobile, $phone, $fax, $imgname);
					
				}
				$this->_redirect('/administrator/contact');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllContactInfo() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/contact?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllContactInfo() as $value) {
					if($this->_request->getParam($value['id']))
					{					
							$model->deleteContact($value['id']);						
					}
				}
				$this->_redirect('/administrator/contact ');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/contact');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	}
}